$(function () {
  Date.prototype.format = function (format) {
    var o = {
      "M+": this.getMonth() + 1, //month
      "d+": this.getDate(), //day
      "h+": this.getHours(), //hour
      "m+": this.getMinutes(), //minute
      "s+": this.getSeconds(), //second
      "q+": Math.floor((this.getMonth() + 3) / 3), //quarter
      S: this.getMilliseconds(), //millisecond
    };
    if (/(y+)/.test(format))
      format = format.replace(
        RegExp.$1,
        (this.getFullYear() + "").substr(4 - RegExp.$1.length)
      );
    for (var k in o)
      if (new RegExp("(" + k + ")").test(format))
        format = format.replace(
          RegExp.$1,
          RegExp.$1.length == 1
            ? o[k]
            : ("00" + o[k]).substr(("" + o[k]).length)
        );
    return format;
  };
  // ----------------------------------------------------------------
  var layer = layui.layer;
  var form = layui.form;
  var laypage = layui.laypage;
  //----------------------------------------------------------------
  // 页面查询参数
  var query = {
    pagenum: 1, //页码值
    pagesize: 10, //每页显示几条数据
    username: "", //姓名
    sno: "", //学号
  };
  //----------------------------------------------------------------
  //渲染分页功能
  function renderPage(total) {
    // console.log(total);
    laypage.render({
      elem: "pageBox", //分页容器的id
      count: total, //数据总数，从服务端得到
      limit: query.pagesize, //每页显示几条数据
      curr: query.pagenum,
      layout: ["count", "limit", "prev", "page", "next", "skip"],
      limits: [10, 12, 15, 20],
      jump: function (obj, first) {
        // console.log(first);
        query.pagenum = obj.curr;
        query.pagesize = obj.limit;
        // 首行不执行
        if (!first) {
          initUsersList();
        }
      },
    });
  }
  //----------------------------------------------------------------
  initUsersList();

  //----------------------------------------------------------------

  //----------------------------------------------------------------
  //获取所有用户信息列表
  function initUsersList() {
    var content = $("#form_search [name = content]").val();
    var submit_time = $("#form_search [name = submit_time]").val();
    var sno = window.parent.$("#sno").val();
    query.content = content;
    query.submit_time = submit_time;
    // query.status = status;
    $.ajax({
      method: "POST",
      url: "/my/userinfo/getSuggestions/",
      data: query,
      success: function (res) {
        const filteredData = res.data.filter((item) => item.status_type === 2);
        // console.log(filteredData);

        var htmStr = template("tpl-table", { data: filteredData });
        $("tbody").html(htmStr);
        renderPage(filteredData.length);
      },
    });
  }
  //----------------------------------------------------------------
  //所有用户信息模糊搜索需要的信息
  $("#form_search").on("submit", function (e) {
    e.preventDefault();
    initUsersList();
  });

  //----------------------------------------------------------------
  // 根据id获取dialog
  var editLab_index = null;
  $("tbody").on("click", ".btn_editLab", function () {
    editLab_index = layer.open({
      type: 1,
      area: ["500px", ""],
      title: "服务评分",
      content: $("#dialog_editLab").html(),
      success: function () {
        // initUsersList()
        form.render();
      },
    });
    var id = $(this).attr("data_id");
    // console.log(id);
    $.ajax({
      method: "GET",
      url: "/my/userinfo/getSuggestionsByID/" + id,
      success: function (response) {
        // console.log(response.data);
        form.val("form_edit", response.data[0]);
      },
    });
  });
  // 根据id修改信息
  $("body").on("submit", "#form_editLab", function (e) {
    e.preventDefault();
    const data = $(this).serialize();
    // console.log(data);
    $.ajax({
      method: "POST",
      url: "/my/userinfo/editSuggestionsFix",
      data: data,
      success: function (res) {
        if (res.status !== 0) {
          return layer.msg(res.message);
        }
        initUsersList();
        layer.msg(res.message);
        layer.close(editLab_index);
      },
    });
  });
  //----------------------------------------------------------------
  // 根据id查看图片
  var viewDevice_index = null;
  $("tbody").on("click", ".btn_viewDevice", function () {
    // // console.log('ok');
    var picture = $(this).attr("data_id");
    // console.log(picture);
    var htmStr = template("tpl_view_device", { picture: picture });
    console.log(htmStr);

    $('[name="view_device').html(htmStr);
    // var device = $('body #img')[0].src;

    // console.log(device);

    viewDevice_index = layer.open({
      type: 1,
      area: ["auto", "auto"],
      title: "查看设备图片",
      content: htmStr,
      success: function () {
        form.render();
      },
    });
  });

  // 根据id开始维修
  $("tbody").on("click", ".btn_startFix", function () {
    var id = $(this).attr("data_id");
    // console.log(id);
    layer.confirm(
      "确定维修？",
      {
        icon: 3,
        title: "提示",
      },
      function (deleteLab_index) {
        $.ajax({
          method: "GET",
          url: "/my/userinfo/startFix/" + id,
          success: function (res) {
            if (res.status !== 0) {
              return layer.msg(res.message);
            }
            layer.msg("👌");
            initUsersList();
            layer.close(deleteLab_index);
          },
        });
      }
    );
  });
  // 根据id完成维修
  $("tbody").on("click", ".btn_finishFix", function () {
    var id = $(this).attr("data_id");
    // console.log(id);
    layer.confirm(
      "确定维修完成？",
      {
        icon: 3,
        title: "提示",
      },
      function (deleteLab_index) {
        $.ajax({
          method: "GET",
          url: "/my/userinfo/finishFix/" + id,
          success: function (res) {
            if (res.status !== 0) {
              return layer.msg(res.message);
            }
            layer.msg("👌");
            initUsersList();
            layer.close(deleteLab_index);
          },
        });
      }
    );
  });
});
